﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.Data.Common;
using System.Data;
using WMSADBUtility;
using WMSAML.SY;

namespace WMSADL.SY
{
    public class TSystemLogInfoDAL
    {
        /// <summary>
        /// 
        /// </summary>
        public bool ExistsByID_SystemLogInfo(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TSystemLogInfo where ID=@ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int Add_SystemLogInfo(TSystemLogInfoEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TSystemLogInfo(");
            strSql.Append("UserID,UserName,Type,OperateDateTime,OperateDesc,Status)");

            strSql.Append(" values (");
            strSql.Append("@UserID,@UserName,@Type,@OperateDateTime,@OperateDesc,@Status)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, entity.UserID);
            db.AddInParameter(dbCommand, "UserName", DbType.String, entity.UserName);
            db.AddInParameter(dbCommand, "Type", DbType.String, entity.Type);
            db.AddInParameter(dbCommand, "OperateDateTime", DbType.DateTime, entity.OperateDateTime);
            db.AddInParameter(dbCommand, "OperateDesc", DbType.String, entity.OperateDesc);
            db.AddInParameter(dbCommand, "Status", DbType.String, entity.Status);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool DeleteByID_SystemLogInfo(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TSystemLogInfo ");
            strSql.Append(" WHERE ID=@ID");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                if (db.ExecuteNonQuery(dbCommand) > 0)
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList_SystemLogInfo(string idlist)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM TSystemLogInfo ");
            strSql.Append(" where ID in (" + idlist + ")  ");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, idlist);
                if (db.ExecuteNonQuery(dbCommand) > 0)
                    return true;
                else
                    return false;
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update_SystemLogInfo(TSystemLogInfoEntity entity)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE TSystemLogInfo SET ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Type=@Type,");
            strSql.Append("OperateDateTime=@OperateDateTime,");
            strSql.Append("OperateDesc=@OperateDesc,");
            strSql.Append("Status=@Status");
            strSql.Append(" WHERE ID=@ID");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, entity.ID);
                db.AddInParameter(dbCommand, "UserID", DbType.Int32, entity.UserID);
                db.AddInParameter(dbCommand, "UserName", DbType.String, entity.UserName);
                db.AddInParameter(dbCommand, "Type", DbType.String, entity.Type);
                db.AddInParameter(dbCommand, "OperateDesc", DbType.String, entity.OperateDesc);
                db.AddInParameter(dbCommand, "OperateDateTime", DbType.DateTime, entity.OperateDateTime);
                db.AddInParameter(dbCommand, "Status", DbType.String, entity.Status);
                if (db.ExecuteNonQuery(dbCommand) > 0)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public TSystemLogInfoEntity GetModel_SystemLogInfo(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  TOP 1 ID,UserID,UserName,Type,OperateDateTime,OperateDesc,Status FROM TSystemLogInfo ");
            strSql.Append(" WHERE ID=@ID");
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                db.AddInParameter(dbCommand, "ID", DbType.Int32, id);
                TSystemLogInfoEntity objInfo = new TSystemLogInfoEntity();
                using (IDataReader dataReader = db.ExecuteReader(dbCommand))
                {
                    if (dataReader.Read())
                    {
                        objInfo.ID = Convert.ToInt32(dataReader["ID"].ToString());
                        objInfo.UserID = Convert.ToInt32(dataReader["UserID"].ToString());
                        objInfo.UserName = dataReader["UserName"].ToString();
                        objInfo.Type = dataReader["Type"].ToString();
                        objInfo.OperateDesc = dataReader["OperateDesc"].ToString();
                        objInfo.OperateDateTime = Convert.ToDateTime(dataReader["OperateDateTime"].ToString());
                        objInfo.Status = dataReader["Status"].ToString();
                    }
                }
                return objInfo;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList_SystemLogInfo(string userName, string time)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            DataSet ds = new DataSet();
            string strWhere = "";
            strSql.Append("SELECT ID,UserID,UserName,Type,OperateDateTime,OperateDesc,Status ");
            strSql.Append(" FROM TSystemLogInfo WHERE Status='是' ");
            if (userName.Trim() != "")
                strWhere += " AND UserName='" + userName + "' ";
            if (time.Trim() != "")
                strWhere += " AND CONVERT(varchar(100), OperateDateTime, 23) ='" + time + "' ";
            if (strWhere != "")
                strSql.Append(strWhere);
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                ds = db.ExecuteDataSet(dbCommand);
                return (ds.Tables[0].Rows.Count > 0) ? ds : null;
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount_SystemLogInfo(string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM TSystemLogInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            using (DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()))
            {
                object obj = db.ExecuteScalar(dbCommand);
                return (obj == null) ? 0 : Convert.ToInt32(obj);
            }
        }
    }
}
